package org.example.lost.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.domain.PageInfoDTO;

import org.example.lost.entity.Role;
import org.example.lost.entity.UserInfo;
import org.example.lost.entity.dto.UserDTO;
import org.example.lost.entity.vo.RoleInfoVO;
import org.example.lost.entity.vo.UserInfoVO;
import org.example.lost.entity.vo.UserLoginVO;
import org.example.lost.entity.vo.UserUpdateVO;
import org.example.lost.mapper.UserMapper;
import org.example.lost.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author hxy
 * @Date 2021/3/21 22:05
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public PageInfoDTO<UserInfo> getList(UserInfoVO userInfoVO) {
        PageHelper.startPage(userInfoVO.getPageNum(),userInfoVO.getPageSize());
        List<UserInfo> userInfoList = userMapper.selectList(userInfoVO);
        PageInfo<UserInfo> pageInfo=new PageInfo<>(userInfoList);
        return PageInfoDTO.to(pageInfo);
    }

    @Override
    public int delete(String userId) {
        int result = userMapper.delete(userId);
        return result;
    }

    @Override
    public UserDTO getOne(String userPhone) {
        UserInfo userInfo = userMapper.selectByPhone(userPhone);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userInfo,userDTO);
        return userDTO;
    }

    @Override
    public int updatePassword(UserUpdateVO userUpdateVO) {
        String userId = userUpdateVO.getUserId();
        UserInfo userInfo = userMapper.selectUserByUserId(userId);
        boolean flag = userUpdateVO.getOldUserPwd().equals(userInfo.getUserPwd());
        if(flag){
            /*String encode = passwordEncoder.encode(userUpdateVO.getNewUserPwd());*/
            int result = userMapper.updatePassword(userUpdateVO.getUserId(),userUpdateVO.getNewUserPwd());
            return result;

        }
        return 2;
    }

    @Override
    public UserInfo getUser(String userId) {
        return userMapper.selectUserByUserId(userId);
    }

    @Override
    public UserInfo login(UserLoginVO userLoginVO) {
        UserInfo userInfo = userMapper.selectByPhone(userLoginVO.getUsername());
        if(userInfo == null){
            return null;
        }
        if(userLoginVO.getPassword().equals(userInfo.getUserPwd())){
            return userInfo;
        }
        return null;
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        userInfo.setUpdateTime(new Date());
        return userMapper.updateUser(userInfo);
    }

    @Override
    public int addUser(UserInfo userInfo) {
        String userId = UUID.randomUUID().toString().replace("-","");
        userInfo.setUserId(userId);
        userInfo.setUpdateTime(new Date());
        userInfo.setCreateTime(new Date());
        userInfo.setUserPwd("123456");
        int result = userMapper.insertUser(userInfo);
        int result2 = userMapper.insertRole(userId);
        if( result== 1 && result2 ==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateRole(RoleInfoVO roleInfoVO) {

        String roleId = userMapper.selectByRoleName(roleInfoVO.getRoleName());
        int result = userMapper.updateRole(roleInfoVO.getUserId(),roleId);
        return result;
    }
}
